<%--
  @desc: 应用系统用户角色绑定
  @author: chenw
  @create: 2014-5-23 AM 9:36
--%>
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<style type="text/css">
    .pageFormContent .textInput {
        width: 220px;
    }

    .gray {
        color: gray;
    }

    #res_detail {
        border: 1px solid #ccc;
        height: 55px;
        clear: right;
        text-indent: 5px;
        padding-top: 3px;
    }
</style>
<div class="pageContent">
    <div class="pageFormContent">
        <div style="height: 340px;overflow-y: auto;margin-bottom: 8px;">
            <ul id="sys_res_tree" class="tree treeFolder treeCheck expand">
                <li>
                    <a href="#">系统角色</a>
                    <ul>
                        <c:forEach var="role" items="${roles}">
                            <li class="leaf">
                                <a href="<c:url value='/sys/resource/bind/${userId}/${role.id}'/>" ${role.flag}
                                   tvalue="${role.id}" remark="${role.remark}" title="${role.remark}">${role.name}</a>
                            </li>
                        </c:forEach>
                    </ul>
                </li>
            </ul>
        </div>
        <div id="res_detail"></div>
    </div>
    <div class="formBar">
        <ul>
            <li>
                <div class="buttonActive">
                    <div class="buttonContent">
                        <button type="button" onclick="saveUserRole();">保存</button>
                    </div>
                </div>
            </li>
            <li>
                <div class="button">
                    <div class="buttonContent">
                        <button type="button" class="close">取消</button>
                    </div>
                </div>
            </li>
        </ul>
    </div>
</div>
<script type="text/javascript">
    function saveUserRole() {
        var ids = [];
        $("#sys_res_tree .leaf .ckbox.checked").each(function (index, obj) {
            ids.push($(this).siblings("a").attr("tvalue"));
        });
       // alert(ids.length);
       if(ids.length>1){
    	   alertMsg.info("禁止选择多个角色");
    	   
       }else if(ids.length==0){
           alertMsg.info("请选择角色");
           return ;
       }
       else{
        $.ajax({
            type: 'post',
            contentType: 'application/json',
            url: '<c:url value='/sys/user/bind/${userId}'/>?ids=' + ids.join(),
            processData: false,
            dataType: 'json',
            success: function (resp) {
                if (resp && resp.statusCode == DWZ.statusCode.ok) {
                    alertMsg.info("用户角色信息更新成功");
                } else {
                    alertMsg.error("用户角色信息更新失败");
                }
            }

        });
       }
    }
    $(function () {
        $("#sys_res_tree a").click(function () {
            $("#res_detail").html($(this).attr("remark"));
        });
    });
</script>